package com.YCH.utils.common;

import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;
import java.io.*;

/**
 * 去除富文本标签工具
 */
public class Html2Text extends HTMLEditorKit.ParserCallback{
    private static Html2Text h2t = new Html2Text();
    private Html2Text(){};
    private StringBuffer s;
    private void parse(String str) throws IOException {
        InputStream iin = new ByteArrayInputStream(str.getBytes());
        Reader in = new InputStreamReader(iin);
        s = new StringBuffer();
        ParserDelegator delegator = new ParserDelegator();
        // the third parameter is TRUE to ignore charset directive
        delegator.parse(in, this, Boolean.TRUE);
        iin.close();
        in.close();
    }
    public void handleText(char[] text, int pos) {
        s.append(text);
    }
    public String getText() {
        return s.toString();
    }
    public static String getContent(String str) {
        try {
            h2t.parse(str);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return h2t.getText();
    }
    public static void main (String[] args) {
        System.out.println(Html2Text.getContent("<p style=\"box-sizing: border-box; margin: 0px 0px 15px; padding: 0px; outline: none; cursor: unset; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; color: #1e1f20; font-family: 'Microsoft YaHei', 微软雅黑, Helvetica, Arial, sans-serif; font-size: 16px; white-space: pre-wrap; background-color: #ffffff;\">崩坏3「星火流音」纪念演出定档10月5日19点30分，进行全球直播，此次我们非常荣幸地邀请到了印象曲的演唱歌手蔡健雅、茶理理、Hanser、尚雯婕、TetraCalyx、小林未郁、袁娅维，一起为大家带来这场特别的演出。</p> <p style=\"box-sizing: border-box; margin: 0px 0px 15px; padding: 0px; outline: none; cursor: unset; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; color: #1e1f20; font-family: 'Microsoft YaHei', 微软雅黑, Helvetica, Arial, sans-serif; font-size: 16px; white-space: pre-wrap; background-color: #ffffff;\">星火辉烁，织梦成歌，希望舰长们能通过这场演出，感受米哈游与《崩坏3》音乐的魅力，共同庆祝五周年的到来！</p>"));
    }
}